package com.zmn.plat.admin.controller.customer;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.user.Customer;

/**
 * @author nowind
 * @since 18/04/19 11:58
 */
@Controller
@RequestMapping("/customer/")
public class CustomerModifyController extends CustomerBaseController {
    private Logger logger = LoggerFactory.getLogger(CustomerModifyController.class);

    // region 修改客户信息
    @RequestMapping(value = "addEdit")
    public ModelAndView addEdit(HttpServletRequest request, Customer customer) {

        String lable = customer.getCustomerId() == 0 ? "添加客户" : "修改客户";

        String mobileStr = request.getParameter("mobileStr").trim();
        if (StringUtil.isNotBlank(mobileStr) && StringUtil.isMobile(mobileStr)) {
            customer.setMobile(Long.parseLong(mobileStr));
        } else if (StringUtil.isNotBlank(mobileStr)) {
            return ajaxDoneError("手机1 号码格式错误！");
        }
        String mobileStr2 = request.getParameter("mobileStr2").trim();
        if (StringUtil.isNotBlank(mobileStr2) && StringUtil.isMobile(mobileStr2)) {
            customer.setMobile2(Long.parseLong(mobileStr2));
        } else if (StringUtil.isNotBlank(mobileStr2)) {
            return ajaxDoneError("手机2 号码格式错误！");
        }
        String mobileStr3 = request.getParameter("mobileStr3").trim();
        if (StringUtil.isNotBlank(mobileStr3) && StringUtil.isMobile(mobileStr3)) {
            customer.setMobile3(Long.parseLong(mobileStr3));
        } else if (StringUtil.isNotBlank(mobileStr3)) {
            return ajaxDoneError("手机3 号码格式错误！");
        }

        setRequestAttributes(request, 143, customer.getCustomerId(), customer.getStatus());

        try {

            customer.setUpdater(getMcStaffRealName());
            customer.setOperatorType(GlobalDict.OPERATOR_TYPE_ADMIN);

            if (customer.getCustomerId() == 0) {
                customerService.insert(customer);
            } else {
                customerService.updateByKey(customer);
            }

            // return ajaxDoneSuccess(String.format("%s成功！", lable));
            return ajaxDoneSuccessAndUrl(String.format("%s成功！", lable), "/customer/detail.action?customerId=" + customer.getCustomerId());

        } catch (Exception ex) {

            logger.error(String.format("[zmn]修改客户信息失败,user:[%s]", JSONObject.toJSONString(customer)));
            return ajaxDoneError(String.format("%s失败！", lable));
        }

    }
    // endregion

    // region 更改可用状态
    @RequestMapping(value = "updateStatus")
    public ModelAndView updateStatus(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "") String ids,
        @RequestParam(value = "status", defaultValue = "0") int status) {

        if (StringUtil.isBlank(ids)) {
            return ajaxDoneError("请选择要更改状态的客户！");
        }

        setRequestAttributes(request, 143, ids, 0);

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);

        customerService.updateStatusBatch(arrayIds, status, getMcStaffRealName());

        return ajaxDoneSuccess("更改客户状态成功！");
    }
    // endregion

}
